<template>
  <view class="page">
    <Header> </Header>
    <view class="main_container">
      <view class="main_content">
        <Breadcrumb :text="'同步句子-句子听选-AI陪练'"> </Breadcrumb>
        <RouterTabs :list="routerList" :current="0"> </RouterTabs>
        <view class="content">
          <SentencePractice
            ref="sentencePractice"
            v-if="componentKey > 0"
            :key="componentKey"
            :type="type"
            :sentence="sentence"
            :bookInfo="bookChapter"
            :chapterInfo="chapterRec"
            :sentenceSort="sentenceSort"
            :sentenceNum="sentenceNum"
            :testFlag="testFlag"
            @change="sentenceChange"
            @save="handleSave"
            @view="handleView"
          ></SentencePractice>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { findSentence, listeningSyncSave } from "@/api/hearing";
export default {
  components: {},
  onLoad(option) {
    this.chapterId = option.chapterId;
    this.init();
  },
  data() {
    return {
      routerList: [
        {
          name: "句子听选",
          url: "pages/sentence/sentenceChoice",
        },
        {
          name: "句子拼写",
          url: "pages/sentence/sentenceTranslation",
        },
        {
          name: "句子跟读",
          url: "pages/sentence/sentenceRead",
        },
      ],
      type: 1,
      chapterId: "",
      sentence: null,
      bookChapter: null,
      chapterRec: null,
      sentenceSort: 0,
      sentenceNum: 0,
      testFlag: false,
      componentKey: 0,
    };
  },
  methods: {
    init() {
      //default
      this.sentenceChange();
    },
    sentenceChange() {
      this.getSentence().then(() => {
        this.componentKey += 1;
      });
    },
    async getSentence() {
      let params = {
        chapterId: this.chapterId,
        type: this.type,
      };
      await findSentence(params).then((res) => {
        this.sentence = res.data.sentence;
        this.bookChapter = res.data.bookChapter;
        this.chapterRec = res.data.chapterRec;
        this.sentenceSort = res.data.sentenceSort;
        this.sentenceNum = res.data.sentenceNum;
        this.testFlag = res.data.testFlag;
      });
    },
    handleSave(flag) {
      let params = {
        chapterId: this.chapterId,
        sentenceId: this.sentence.id,
        correctFlag: flag,
        type: this.type,
      };
      listeningSyncSave(params);
    },
    handleView() {
      uni.redirectTo({
        url: `/pages/sentence/sentenceChoiceAfterSchool?chapterId=${this.chapterId}`,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "./sentenceChoiceDetail";
</style>
